export default class ActionRouter {
    constructor (action_config) {
        this.routers = new Map()
        this.index = null
        this.current_action = null
        this.build(action_config)
    }
    build (config) {
        let temp = config.split('|')
        if (temp.length === 0) {
            throw '未指定Action'
        }
        temp.forEach((step, key) => {
            let t = step.split('=>')
            let tt = new Map()
            if (t.length > 1) {
                let results = t[1].split(',')
                for (let i = 0; i < results.length; i++) {
                    let result = results[i]
                    let [rs, act] = result.split(':')
                    tt.set(rs, act)
                }
            }
            if (key < temp.length - 1) {
                let defalt = temp[key + 1]
                let action = defalt.split('=>')
                tt.set('__default__', action[0])
            }
            this.routers.set(t[0], tt)
            if( key === 0) {
                this.index = t[0]
            }
        })
    }
    route (url) {
        if (url == undefined || url == '') {
            this.current_action = this.index
            return this.index
        }
        if (this.routers.get(this.current_action).has(url)) {
            let target = this.routers.get(this.current_action).get(url)
            this.current_action = target
            return target
        }
        if(this.routers.get(this.current_action).get('__default__')) {
            let target = this.routers.get(this.current_action).get('__default__')
            this.current_action = target
            return target
        }
        console.log(this.routers)
        return undefined
    }

}